library(foreign)
library(zoo)

# This file reads in the raw Eurobarometer data files, extracts a set of
# common variables, merges them together, and then writes out the resulting
# data to a file that the "eurobarometer.r" analysis file makes use of.
# The version of this replication archive that is made public does *not*
# have these Eurobarometer source data files as we felt it inappropriate
# to distribute them in this way. They are easily accessible via the usual
# online sources, however.
# People wishing to fully replicate our analysis - in the sense of obtaining
# the source Eurobarometer files for themselves - should do so, and then
# place them in file locations of the following form:
#   "../source_data/eurobarometer/XX.X/XX.X.dta"
# N.b. the code below expects file names to be changed to the Eurobarometer
# survey number (e.g. "73.4.dta").

ebsurveys = list("73.4"=list(date="May 2010",
                             eurostat_data_quarter=as.yearqtr("2009Q4"),
                             ctryname="v6",
                             ctrycode="v7",
                             polDiscussNational="v92",
                             polDiscussEU="v93",
                             polDiscussLocal="v94",
                             reduceDebtNoDelay="v404",
                             reduceDebtNotPriority="v405",
                             #reduceDebtRapidly="v406",
                             crisisJobsWorstToCome="v366",
                             weightNat="v8",
                             weightUK="v10",
                             weightDE="v12",
                             weightEU27="v40",
                             econNatCurrent="v96",
                             econNatEmplCurrent="v101",
                             econHHFinanCurrent="v100",
                             econPerEmplCurrent="v99",
                             econNatExpect="v123",
                             econNatEmplExpect="v125",
                             econHHFinanExpect="v124",
                             econPerEmplExpect="v126",
                             lrself="v548",
                             gender="v555",
                             eduAgeRecoded="v554",
                             occup="v559",
                             occupPrev="v560",
                             age="v556",
                             hhComp15plus="v562",
                             hhComp10minus="v564",
                             maritalStatusRecoded="v552"),
                 "74.2"=list(date="Nov-Dec 2010",
                             eurostat_data_quarter=as.yearqtr("2010Q2"),
                             ctryname="v6",
                             ctrycode="v7",
                             polDiscussNational="v92",
                             polDiscussEU="v93",
                             polDiscussLocal="v94",
                             reduceDebtNoDelay="v527",
                             reduceDebtNotPriority="v528",
                             crisisJobsWorstToCome="v506",
                             weightNat="v8",
                             weightUK="v10",
                             weightDE="v12",
                             weightEU27="v38",
                             econNatCurrent="v96",
                             econNatEmplCurrent="v101",
                             econHHFinanCurrent="v100",
                             econPerEmplCurrent="v99",
                             econNatExpect="v109",
                             econNatEmplExpect="v111",
                             econHHFinanExpect="v110",
                             econPerEmplExpect="v112",
                             lrself="v594",
                             gender="v602",
                             eduAgeRecoded="v601",
                             occup="v606",
                             occupPrev="v608",
                             age="v603",
                             hhComp15plus="v611",
                             hhComp10minus="v613",
                             maritalStatusRecoded="v598"),
                 "75.3"=list(date="May 2011",
                             eurostat_data_quarter=as.yearqtr("2010Q4"),
                             ctryname="v6",
                             ctrycode="v7",
                             polDiscussNational="v110",
                             polDiscussEU="v111",
                             polDiscussLocal="v112",
                             reduceDebtNoDelay="v480",
                             reduceDebtNotPriority="v481",
                             crisisJobsWorstToCome="v458",
                             weightNat="v8",
                             weightUK="v10",
                             weightDE="v12",
                             weightEU27="v42",
                             econNatCurrent="v114",
                             econNatEmplCurrent="v119",
                             econHHFinanCurrent="v118",
                             econPerEmplCurrent="v117",
                             econNatExpect="v127",
                             econNatEmplExpect="v129",
                             econHHFinanExpect="v128",
                             econPerEmplExpect="v130",
                             lrself="v607",
                             gender="v615",
                             eduAgeRecoded="v614",
                             occup="v619",
                             occupPrev="v621",
                             age="v616",
                             hhComp15plus="v624",
                             hhComp10minus="v626",
                             maritalStatusRecoded="v610"),
                 "76.3"=list(date="November 2011",
                             eurostat_data_quarter=as.yearqtr("2011Q2"),
                             ctryname="country",
                             ctrycode="isocntry",
                             reduceDebtNoDelay="qc4a_3",
                             reduceDebtNotPriority="qc4a_4",
                             crisisJobsWorstToCome="qc1",
                             weightNat="w1",
                             weightUK="w4",
                             weightDE="w3",
                             weightEU27="w22",
                             polDiscussNational="qa2_1",
                             polDiscussEU="qa2_2",
                             polDiscussLocal="qa2_3",
                             econNatCurrent="qa4a_1",
                             econNatEmplCurrent="qa4a_6",
                             econHHFinanCurrent="qa4a_5",
                             econPerEmplCurrent="qa4a_4",
                             econNatExpect="qa5a_2",
                             econNatEmplExpect="qa5a_4",
                             econHHFinanExpect="qa5a_3",
                             econPerEmplExpect="qa5a_5",
                             #lrself="",
                             gender="d10",
                             eduAgeRecoded="d8r1",
                             occup="d15a",
                             occupPrev="d15b",
                             age="d11",
                             hhComp15plus="d40a",
                             hhComp10minus="d40b",
                             maritalStatusRecoded="d7r1"),
                 "77.3"=list(date="May 2012",
                             eurostat_data_quarter=as.yearqtr("2011Q4"),
                             ctryname="country",
                             ctrycode="isocntry",
                             polDiscussNational="qa2_1",
                             polDiscussEU="qa2_2",
                             polDiscussLocal="qa2_3",
                             reduceDebtNoDelay="qc4a_3",
                             reduceDebtNotPriority="qc4a_4",
                             #reduceDebtForGrowth="qc4a_5",
                             crisisJobsWorstToCome="qc1",
                             weightNat="w1",
                             weightUK="w4",
                             weightDE="w3",
                             weightEU27="w22",
                             econNatCurrent="qa4a_1",
                             econNatEmplCurrent="qa4a_6",
                             econHHFinanCurrent="qa4a_5",
                             econPerEmplCurrent="qa4a_4",
                             econNatExpect="qa5a_2",
                             econNatEmplExpect="qa5a_4",
                             econHHFinanExpect="qa5a_3",
                             econPerEmplExpect="qa5a_5",
                             #lrself="",
                             gender="d10",
                             eduAgeRecoded="d8r1",
                             occup="d15a",
                             occupPrev="d15b",
                             age="d11",
                             hhComp15plus="d40a",
                             hhComp10minus="d40b",
                             maritalStatusRecoded="d7r1"),
                 "78.1"=list(date="November 2012",
                             eurostat_data_quarter=as.yearqtr("2012Q2"),
                             ctryname="country",
                             ctrycode="isocntry",
                             polDiscussNational="qa2_1",
                             polDiscussEU="qa2_2",
                             polDiscussLocal="qa2_3",
                             reduceDebtNoDelay="qc4_3",
                             reduceDebtNotPriority="qc4_4",
                             #reduceDebtForGrowth="qc4a_5",
                             crisisJobsWorstToCome="qc1",
                             weightNat="w1",
                             weightUK="w4",
                             weightDE="w3",
                             weightEU27="w22",
                             econNatCurrent="qa3a_1",
                             econNatEmplCurrent="qa3a_6",
                             econHHFinanCurrent="qa3a_5",
                             econPerEmplCurrent="qa3a_4",
                             econNatExpect="qa4a_2",
                             econNatEmplExpect="qa4a_4",
                             econHHFinanExpect="qa4a_3",
                             econPerEmplExpect="qa4a_5",
                             #lrself="",
                             gender="d10",
                             eduAgeRecoded="d8r1",
                             occup="d15a",
                             occupPrev="d15b",
                             age="d11",
                             hhComp15plus="d40a",
                             hhComp10minus="d40b",
                             maritalStatusRecoded="d7r1"),
                 "79.3"=list(date="May 2013",
                             eurostat_data_quarter=as.yearqtr("2012Q4"),
                             ctryname="country",
                             ctrycode="isocntry",
                             polDiscussNational="qa2_1",
                             polDiscussEU="qa2_2",
                             polDiscussLocal="qa2_3",
                             reduceDebtNoDelay="qc4a_3",
                             reduceDebtNotPriority="qc4a_4",
                             #reduceDebtForGrowth="qc4a_5",
                             crisisJobsWorstToCome="qc1",
                             weightNat="w1",
                             weightUK="w4",
                             weightDE="w3",
                             weightEU27="w22",
                             econNatCurrent="qa3a_1",
                             econNatEmplCurrent="qa3a_6",
                             econHHFinanCurrent="qa3a_5",
                             econPerEmplCurrent="qa3a_4",
                             econNatExpect="qa4a_2",
                             econNatEmplExpect="qa4a_4",
                             econHHFinanExpect="qa4a_3",
                             econPerEmplExpect="qa4a_5",
                             #lrself="",
                             gender="d10",
                             eduAgeRecoded="d8r1",
                             occup="d15a",
                             occupPrev="d15b",
                             age="d11",
                             hhComp15plus="d40a",
                             hhComp10minus="d40b",
                             maritalStatusRecoded="d7r1"),
                 "80.1"=list(date="November 2013",
                             eurostat_data_quarter=as.yearqtr("2013Q2"),
                             ctryname="country",
                             ctrycode="isocntry",
                             #polDiscussNational="qa2_1",
                             #polDiscussEU="qa2_2",
                             #polDiscussLocal="qa2_3",
                             reduceDebtNoDelay="qc4_3",
                             reduceDebtNotPriority="qc4_4",
                             #reduceDebtForGrowth="qc4a_5",
                             crisisJobsWorstToCome="qc1",
                             weightNat="w1",
                             weightUK="w4",
                             weightDE="w3",
                             weightEU27="w22",
                             econNatCurrent="qa2a_1",
                             econNatEmplCurrent="qa2a_4",
                             econHHFinanCurrent="qa2a_4",
                             econPerEmplCurrent="qa2a_3",
                             econNatExpect="qa3a_2",
                             econNatEmplExpect="qa3a_4",
                             econHHFinanExpect="qa3a_3",
                             econPerEmplExpect="qa3a_5",
                             #lrself="",
                             gender="d10",
                             eduAgeRecoded="d8r1",
                             occup="d15a",
                             occupPrev="d15b",
                             age="d11",
                             hhComp15plus="d40a",
                             hhComp10minus="d40b",
                             maritalStatusRecoded="d7r1"),
                 "81.4"=list(date="May-June 2014",
                             eurostat_data_quarter=as.yearqtr("2013Q4"),
                             ctryname="country",
                             ctrycode="isocntry",
                             polDiscussNational="d71a_1",
                             polDiscussEU="d71a_2",
                             polDiscussLocal="d71a_3",
                             reduceDebtNoDelay="qc3_3",
                             reduceDebtNotPriority="qc3_4",
                             #reduceDebtForGrowth="qc4a_5",
                             crisisJobsWorstToCome="qc1",
                             weightNat="w1",
                             weightUK="w4",
                             weightDE="w3",
                             weightEU27="w22",
                             econNatCurrent="qa2a_1",
                             econNatEmplCurrent="qa2a_5",
                             econHHFinanCurrent="qa2a_4",
                             econPerEmplCurrent="qa2a_3",
                             econNatExpect="qa3a_2",
                             econNatEmplExpect="qa3a_4",
                             econHHFinanExpect="qa3a_3",
                             econPerEmplExpect="qa3a_5",
                             lrself="d1",
                             gender="d10",
                             eduAgeRecoded="d8r1",
                             occup="d15a",
                             occupPrev="d15b",
                             age="d11",
                             hhComp15plus="d40a",
                             hhComp10minus="d40b",
                             maritalStatusRecoded="d7r1"),
                 "82.3"=list(date="November 2014",
                             eurostat_data_quarter=as.yearqtr("2014Q2"),
                             ctryname="country",
                             ctrycode="isocntry",
                             polDiscussNational="d71a_1",
                             polDiscussEU="d71a_2",
                             polDiscussLocal="d71a_3",
                             reduceDebtNoDelay="qc2_2",
                             reduceDebtNotPriority="qc2_3",
                             #reduceDebtForGrowth="qc4a_5",
                             crisisJobsWorstToCome="qc1",
                             privBetterCreateJobs="qc2_5",
                             pubMoneyStimPrivInvestment="qc2_6",
                             weightNat="w1",
                             weightUK="w4",
                             weightDE="w3",
                             weightEU27="w22",
                             econNatCurrent="qa1a_1",
                             econNatEmplCurrent="qa1a_5",
                             econHHFinanCurrent="qa1a_4",
                             econPerEmplCurrent="qa1a_3",
                             econNatExpect="qa2a_2",
                             econNatEmplExpect="qa2a_4",
                             econHHFinanExpect="qa2a_3",
                             econPerEmplExpect="qa2a_5",
                             lrself="d1",
                             gender="d10",
                             eduAgeRecoded="d8r1",
                             occup="d15a",
                             occupPrev="d15b",
                             age="d11",
                             hhComp15plus="d40a",
                             hhComp10minus="d40b",
                             maritalStatusRecoded="d7r1"),
                 "83.3"=list(date="May 2015",
                             eurostat_data_quarter=as.yearqtr("2014Q4"),
                             ctryname="country",
                             ctrycode="isocntry",
                             polDiscussNational="d71a_1",
                             polDiscussEU="d71a_2",
                             polDiscussLocal="d71a_3",
                             reduceDebtNoDelay="qc2a_1",
                             reduceDebtNotPriority="qc2a_2",
                             #reduceDebtForGrowth="qc4a_5",
                             crisisJobsWorstToCome="qc1",
                             privBetterCreateJobs="qc2a_4",
                             pubMoneyStimPrivInvestment="qc2a_5",
                             weightNat="w1",
                             weightUK="w4",
                             weightDE="w3",
                             weightEU27="w22",
                             econNatCurrent="qa1a_1",
                             econNatEmplCurrent="qa1a_5",
                             econHHFinanCurrent="qa1a_4",
                             econPerEmplCurrent="qa1a_3",
                             econNatExpect="qa2a_2",
                             econNatEmplExpect="qa2a_4",
                             econHHFinanExpect="qa2a_3",
                             econPerEmplExpect="qa2a_5",
                             lrself="d1",
                             gender="d10",
                             eduAgeRecoded="d8r1",
                             occup="d15a",
                             occupPrev="d15b",
                             age="d11",
                             hhComp15plus="d40a",
                             hhComp10minus="d40b",
                             maritalStatusRecoded="d7r1"),
                 "84.3"=list(date="November 2015",
                             eurostat_data_quarter=as.yearqtr("2015Q2"),
                             ctryname="country",
                             ctrycode="isocntry",
                             polDiscussNational="d71a_1",
                             polDiscussEU="d71a_2",
                             polDiscussLocal="d71a_3",
                             reduceDebtNoDelay="qc2a_1",
                             reduceDebtNotPriority="qc2a_2",
                             #reduceDebtForGrowth="qc4a_5",
                             crisisJobsWorstToCome="qc1",
                             privBetterCreateJobs="qc2a_4",
                             pubMoneyStimPrivInvestment="qc2a_5",
                             weightNat="w1",
                             weightUK="w4",
                             weightDE="w3",
                             weightEU27="w22",
                             econNatCurrent="qa1a_1",
                             econNatEmplCurrent="qa1a_5",
                             econHHFinanCurrent="qa1a_4",
                             econPerEmplCurrent="qa1a_3",
                             econNatExpect="qa2a_2",
                             econNatEmplExpect="qa2a_4",
                             econHHFinanExpect="qa2a_3",
                             econPerEmplExpect="qa2a_5",
                             lrself="d1",
                             gender="d10",
                             eduAgeRecoded="d8r1",
                             occup="d15a",
                             occupPrev="d15b",
                             age="d11",
                             hhComp15plus="d40a",
                             hhComp10minus="d40b",
                             maritalStatusRecoded="d7r1"),
                 "85.2"=list(date="May 2016",
                             eurostat_data_quarter=as.yearqtr("2015Q4"),
                             ctryname="country",
                             ctrycode="isocntry",
                             polDiscussNational="d71a_1",
                             polDiscussEU="d71a_2",
                             polDiscussLocal="d71a_3",
                             reduceDebtNoDelay="qc2a_1",
                             reduceDebtNotPriority="qc2a_2",
                             #reduceDebtForGrowth="qc4a_5",
                             crisisJobsWorstToCome="qc1",
                             privBetterCreateJobs="qc2a_4",
                             pubMoneyStimPrivInvestment="qc2a_5",
                             weightNat="w1",
                             weightUK="w4",
                             weightDE="w3",
                             weightEU27="w22",
                             econNatCurrent="qa1a_1",
                             econNatEmplCurrent="qa1a_5",
                             econHHFinanCurrent="qa1a_4",
                             econPerEmplCurrent="qa1a_3",
                             econNatExpect="qa2a_2",
                             econNatEmplExpect="qa2a_4",
                             econHHFinanExpect="qa2a_3",
                             econPerEmplExpect="qa2a_5",
                             lrself="d1",
                             gender="d10",
                             eduAgeRecoded="d8r1",
                             occup="d15a",
                             occupPrev="d15b",
                             age="d11",
                             hhComp15plus="d40a",
                             hhComp10minus="d40b",
                             maritalStatusRecoded="d7r1"),
                 "86.2"=list(date="November 2016",
                             eurostat_data_quarter=as.yearqtr("2016Q2"),
                             ctryname="country",
                             ctrycode="isocntry",
                             polDiscussNational="d71a_1",
                             polDiscussEU="d71a_2",
                             polDiscussLocal="d71a_3",
                             reduceDebtNoDelay="qc2_1",
                             reduceDebtNotPriority="qc2_2",
                             #reduceDebtForGrowth="qc4a_5",
                             crisisJobsWorstToCome="qc1",
                             privBetterCreateJobs="qc2_4",
                             pubMoneyStimPrivInvestment="qc2_5",
                             weightNat="w1",
                             weightUK="w4",
                             weightDE="w3",
                             weightEU27="w22",
                             econNatCurrent="qa1a_1",
                             econNatEmplCurrent="qa1a_5",
                             econHHFinanCurrent="qa1a_4",
                             econPerEmplCurrent="qa1a_3",
                             econNatExpect="qa2a_2",
                             econNatEmplExpect="qa2a_4",
                             econHHFinanExpect="qa2a_3",
                             econPerEmplExpect="qa2a_5",
                             lrself="d1",
                             gender="d10",
                             eduAgeRecoded="d8r1",
                             occup="d15a",
                             occupPrev="d15b",
                             age="d11",
                             hhComp15plus="d40a",
                             hhComp10minus="d40b",
                             maritalStatusRecoded="d7r1"),
                 "87.3"=list(date="May 2017",
                             eurostat_data_quarter=as.yearqtr("2016Q4"),
                             ctryname="country",
                             ctrycode="isocntry",
                             polDiscussNational="d71a_1",
                             polDiscussEU="d71a_2",
                             polDiscussLocal="d71a_3",
                             reduceDebtNoDelay="qc2a_1",
                             reduceDebtNotPriority="qc2a_2",
                             #reduceDebtForGrowth="qc4a_5",
                             crisisJobsWorstToCome="qc1",
                             privBetterCreateJobs="qc2a_4",
                             pubMoneyStimPrivInvestment="qc2a_5",
                             weightNat="w1",
                             weightUK="w4",
                             weightDE="w3",
                             weightEU27="w22",
                             econNatCurrent="qa1a_1",
                             econNatEmplCurrent="qa1a_5",
                             econHHFinanCurrent="qa1a_4",
                             econPerEmplCurrent="qa1a_3",
                             econNatExpect="qa2a_2",
                             econNatEmplExpect="qa2a_4",
                             econHHFinanExpect="qa2a_3",
                             econPerEmplExpect="qa2a_5",
                             lrself="d1",
                             gender="d10",
                             eduAgeRecoded="d8r1",
                             occup="d15a",
                             occupPrev="d15b",
                             age="d11",
                             hhComp15plus="d40a",
                             hhComp10minus="d40b",
                             maritalStatusRecoded="d7r1"),
                 "88.3"=list(date="November 2017",
                             eurostat_data_quarter=as.yearqtr("2017Q2"),
                             ctryname="country",
                             ctrycode="isocntry",
                             polDiscussNational="d71a_1",
                             polDiscussEU="d71a_2",
                             polDiscussLocal="d71a_3",
                             reduceDebtNoDelay="qc2a_1",
                             reduceDebtNotPriority="qc2a_2",
                             #reduceDebtForGrowth="qc4a_5",
                             crisisJobsWorstToCome="qc1",
                             privBetterCreateJobs="qc2a_4",
                             pubMoneyStimPrivInvestment="qc2a_5",
                             weightNat="w1",
                             weightUK="w4",
                             weightDE="w3",
                             weightEU27="w22",
                             econNatCurrent="qa1a_2",
                             econNatEmplCurrent="qa1a_6",
                             econHHFinanCurrent="qa1a_5",
                             econPerEmplCurrent="qa1a_4",
                             econNatExpect="qa2a_3",
                             econNatEmplExpect="qa2a_5",
                             econHHFinanExpect="qa2a_4",
                             econPerEmplExpect="qa2a_6",
                             lrself="d1",
                             gender="d10",
                             eduAgeRecoded="d8r1",
                             occup="d15a",
                             occupPrev="d15b",
                             age="d11",
                             hhComp15plus="d40a",
                             hhComp10minus="d40b",
                             maritalStatusRecoded="d7r1")
)

# Hack to initialize data frame with column names I want. Seems to need empty first row to work.
ebcombi = data.frame(foo=c(NA))
for (coln in c(names(ebsurveys[["87.3"]]), "srv")) {
  ebcombi[[coln]] = c(NA)
}
# Delete the extraneous initialization column
ebcombi$foo <- NULL

for (srv in names(ebsurveys)) {		
  fp = paste("../source_data/eurobarometer", srv, sep="/")
  fp = paste(fp, srv, sep="/")
  fp = paste(fp, ".dta", sep="")
  dta = read.dta(fp, convert.factors=FALSE)
  colnames_new = names(ebsurveys[[srv]])
  # Strip off the leading "date" and "eurostat_data_quarter" columns as they don't map to a EB survey column.
  colnames_new = colnames_new[3:length(colnames_new)]
  # create name vector with survey-specific column names
  colnames_old = c()
  for (newname in colnames_new) {
    colnames_old = append(colnames_old, ebsurveys[[srv]][[newname]])
  }
  # pull appropriate variables/columns from the full eb data
  dta_subset = subset(dta, select=colnames_old)
  # assign new (informative, common across surveys) column names
  names(dta_subset) <- colnames_new
  # add a column to the data indicating survey number
  dta_subset$srv <- rep(srv, length(dta_subset$ctrycode))
  dta_subset$date <- rep(ebsurveys[[srv]][["date"]], length(dta_subset$ctrycode)) 
  # add back economic data quarter that respondents will have "seen" when taking the survey
  dta_subset$eurostat_data_quarter <- rep(ebsurveys[[srv]][["eurostat_data_quarter"]], length(dta_subset$ctrycode))
  # Some logic to add missing values for columns that are not present in all surveys so that rbind doesn't throw an error.
  missing_cols_idxs = which(names(ebcombi) %in% names(dta_subset) == FALSE)
  for (col_idx in missing_cols_idxs) {
    coln = names(ebcombi)[col_idx]
    dta_subset[[coln]] = rep(NA, times=length(dta_subset$ctryname))
  }
  ebcombi = rbind(ebcombi, dta_subset)
  
  print(srv)
}
# Delete the extraneous initialization row
ebcombi = ebcombi[-c(1),]

# dates as dates (date and year) of survey questions 
split.date = strsplit(ebcombi$date, split = " ")
split.date.mat = matrix(unlist(split.date), ncol = 2, byrow = T)

ebcombi$smonth = rep(NA, nrow(ebcombi))
ebcombi$smonth[which(split.date.mat[ , 1] == "May")] = "05"
ebcombi$smonth[which(split.date.mat[ , 1] == "May-June")] = "05"
ebcombi$smonth[which(split.date.mat[ , 1] == "November")] = "11"
ebcombi$smonth[which(split.date.mat[ , 1] == "Nov-Dec")] = "11"

ebcombi$syear = split.date.mat[,2]

split.date.vec = paste("01", ebcombi$smonth, ebcombi$syear, sep = "-")
ebcombi$sdate = as.Date(split.date.vec, format = "%d-%m-%Y")

# higher values, more concern about defitics? greater agreement with statement, i.e. prodef higher values are disagreement with reduce debt no delay. 
ebcombi$antidef = ebcombi$reduceDebtNotPriority
ebcombi$prodef = ebcombi$reduceDebtNoDelay

ebcombi$antidef[which(ebcombi$antidef > 5)] <- NA
ebcombi$prodef[which(ebcombi$prodef > 5)] <- NA

# indicator for which deficit question

ebcombi$defsplitb = rep(NA, nrow(ebcombi))
ebcombi$defsplitb[which(ebcombi$prodef <= 5)] = 1
ebcombi$defsplitb[which(ebcombi$antidef <= 5)] = 0

# Recode remaining (post 81.4) DK debt attitudes to missing
ebcombi$antidef[which(ebcombi$antidef == 5)] <- NA
ebcombi$prodef[which(ebcombi$prodef == 5)] <- NA
# Combine two scales, reversing the prodef one
ebcombi$antidef[which(is.na(ebcombi$antidef))] <- 5 - ebcombi$prodef[which(is.na(ebcombi$antidef))]

ebcombi$antidef.o = as.ordered(ebcombi$antidef)



for (econvar in c("econNatCurrent", "econNatEmplCurrent", "econHHFinanCurrent", "econPerEmplCurrent", "econNatExpect", "econNatEmplExpect", "econHHFinanExpect", "econPerEmplExpect")) {
  econvar_nodk = paste(econvar, "_rev_nodk", sep="")
  # Reverse the coding so that higher values correspond to better economy
  ebcombi[[econvar_nodk]] <- 5 - ebcombi[[econvar]]
  # N.b. 0 is now DK as we reversed the scale
  ebcombi[[econvar_nodk]][which(ebcombi[[econvar_nodk]] == 0)] <- NA
}

for (discussvar in c("polDiscussNational", "polDiscussEU", "polDiscussLocal")) {
  discussvar_nodk = paste(discussvar, "_rev_nodk", sep="")
  # Remove DK (4) and Inap. (9) responses
  ebcombi[[discussvar_nodk]] <- ebcombi[[discussvar]]
  ebcombi[[discussvar_nodk]][which(ebcombi[[discussvar]] %in% c(4,9))] <- NA
  # Reverse the coding so that higher values correspond to better economy
  ebcombi[[discussvar_nodk]] <- 3 - ebcombi[[discussvar_nodk]]
}

# Setup geo variable to provide for merging with Eurostat data
ebcombi$geo = ebcombi$ctrycode
ebcombi$geo[which(ebcombi$geo %in% c("DE-W", "DE-E"))] = "DE"
ebcombi$geo[which(ebcombi$geo %in% c("GB-GBN", "GB-NIR"))] = "UK"
ebcombi$geo[which(ebcombi$geo %in% c("GR"))] = "EL"

ebcombi$quarter = ebcombi$eurostat_data_quarter
ebcombi$year = floor(ebcombi$quarter)

ebcombi$lr3 = rep(NA, nrow(ebcombi))
ebcombi$lr3[which(ebcombi$lrself >= 6)]  = 2
ebcombi$lr3[which(ebcombi$lrself < 6)] = 1
ebcombi$lr3[which(ebcombi$lrself < 5)] = 0
ebcombi$lr3[which(ebcombi$lrself > 10)] = NA

eb.uk = subset(ebcombi, geo == "UK")

# Write out eb.uk to a file so that we can provide it easily for the replication archive;
# i.e. without having to distribute the full set of Eurobarometer source files.
save(eb.uk, file="../generated_data/eb_uk.rdata")

